package com.digua.atools.tools;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

/**
 * @author:digua
 * @dat:2021/8/30
 * @description: 应用功具类
 */
public class AppUtil {
    /**
     * 判断当前应用是否是debug状态
     */
    public static boolean isDebug(Context context) {
        try {
            ApplicationInfo info = context.getApplicationInfo();
            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 判断当前版本是否大于要比较的版本
     *
     * @param version 要比较的版本
     * @return true:当前版本大; false: 传入的版本大
     */
    public static boolean isVersionGt(String version) {
        if (StrUtil.isBlank(version)) {
            return true;
        }
        String curVer = AppUtils.getAppVersionName();
        String[] curVers = curVer.split("\\.");
        String[] vers = version.split("\\.");
        for (int i = 0; i < curVers.length; i++) {
            if (vers.length <= i) {
                return true;
            }
            String curV = NumUtil.parseInt(curVers[i]) + "";
            String v = NumUtil.parseInt(vers[i]) + "";
            if (curV.length() > v.length()) {
                v += StrUtil.getPlaceholder(curV.length() - v.length(), "0");
            } else if (curV.length() < v.length()) {
                curV += StrUtil.getPlaceholder(curV.length() - v.length(), "0");
            }
            if (ObjUtil.eq(curV, v)) {
                continue;
            }
            return NumUtil.gt(curV, v);
        }
        return true;
    }

    /**
     * 判断当前版本是否大于要比较的版本(前面的大返回false;反之返回true)
     *
     * @param version      要比较的版本
     * @param robotVersion 当前设备版本号
     * @return true:robotVersion版本大; false: 传入的版本大
     */
    public static boolean isVersionGt(String version, String robotVersion) {
        if (StrUtil.isBlank(version)) {
            return true;
        }
        String curVer = StrUtil.isBlank(robotVersion) ? AppUtils.getAppVersionName() : robotVersion;
        String[] curVers = curVer.split("\\.");
        String[] vers = version.split("\\.");
        for (int i = 0; i < curVers.length; i++) {
            if (vers.length <= i) {
                return true;
            }
            String curV = NumUtil.parseInt(curVers[i], -10) + "";
            String v = NumUtil.parseInt(vers[i], -10) + "";
            boolean isStrSort = ObjUtil.eq(curV, "-10") || ObjUtil.eq(v, "-10");
            if (isStrSort) {
                curV = curVers[i];
                v = vers[i];
            }
            if (curV.length() > v.length()) {
                v += StrUtil.getPlaceholder(curV.length() - v.length(), "0");
            } else if (curV.length() < v.length()) {
                curV += StrUtil.getPlaceholder(curV.length() - v.length(), "0");
            }
            if (ObjUtil.eq(curV, v)) {
                continue;
            }
            if (isStrSort) {
                String[] arr = {curV, v};
                Arrays.sort(arr);
                return ObjUtil.eq(curV, arr[1]);
            } else {
                return NumUtil.gt(curV, v);
            }
        }
        return true;
    }

    /**
     * 获取全局的application
     *
     * @return 返回application
     */
    public static Application getApplication() {
        try {
            @SuppressLint("PrivateApi")
            Class<?> activityThread = Class.forName("android.app.ActivityThread");
            Object thread = activityThread.getMethod("currentActivityThread").invoke(null);
            Object app = activityThread.getMethod("getApplication").invoke(thread);
            if (app == null) {
                throw new NullPointerException("u should init first");
            }
            return (Application) app;
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        throw new NullPointerException("u should init first");
    }

    /**
     * 判断当前Activity是否在栈顶
     *
     * @param activity
     * @return
     */
    public static boolean isCurActivity(Activity activity) {
        if (activity == null) {
            return false;
        }
        return ActivityUtils.getTopActivity() == activity;
    }
}
